<?php
 
 define ('MODEL_NEWS_FIND', 	'SELECT `id`, DATE_FORMAT(`date`,"%e.%m.%Y") as `date`, `description` FROM `news` ORDER BY `date` ');
 define ('MODEL_NEWS_FIND_ONE', 'SELECT `id`, DATE_FORMAT(`date`,"%e.%m.%Y") as `date`, `description`, `text` FROM `news` WHERE `id` = ?');
 define ('MODEL_NEWS_ADD', 		'INSERT INTO `news`(`description`,`date`) VALUE (?, now())');
 
 define ('MODEL_NEWS_SAVE',		'UPDATE `news` SET `description` = ?, `date` =  STR_TO_DATE(?, "%d.%m.%Y"), `text` = ? WHERE `id` = ?');
 define ('MODEL_NEWS_DELETE',	'DELETE FROM `news` WHERE `id` = ?');
 
 class Model_News extends Models_Base {
 	
 	function find($args = null) {
 		if($args[id])	
 			return $this->getRow(MODEL_NEWS_FIND_ONE,array($args[id]));
 		else if($args[limit]) {
		    
		    $sth 	= $this->dbh->prepare(MODEL_NEWS_FIND);
		    
		    $start 	= $args[start] ? $args[start] : 0;
 			
 			$sth->bindValue(':start', $start,		PDO::PARAM_INT);
 			$sth->bindValue(':limit', $args[limit],	PDO::PARAM_INT);		    

			$sth->execute();
			
		    return $sth->fetchAll(); 			

 		} else {
 			throw new Exception ('wrong arguments in news->find');
 		}
 			
 	}
 	
 	function add($description) {
 		return $this->execute(MODEL_NEWS_ADD,array($description));
 	}
 	
 	function save($args) {
 		return $this->execute(MODEL_NEWS_SAVE,array($args[description],$args[date],$args[Text],$args[id]));
 	}
 	
 	function delete($id) {
 		return $this->execute(MODEL_NEWS_DELETE,array($id));
 	}
 	
 }